package com.graduate.cec.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.FeedbackMapper;
import com.graduate.cec.mapper.UserMapper;
import com.graduate.cec.pojo.dto.FeedbackSimpleDTO;
import com.graduate.cec.pojo.entity.Feedback;
import com.graduate.cec.pojo.entity.FeedbackExample;
import com.graduate.cec.pojo.entity.User;
import com.graduate.cec.pojo.vo.FeedbackVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class FeedbackService {

    @Resource
    private FeedbackMapper feedbackMapper;

    @Resource
    private UserMapper userMapper;

    /****************        增       ******************************************************************/
    public void insertFeedback(Feedback feedback){
        log.info("返回的feedback:{}",feedback);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String phone = (String) principal;
        Long id = userMapper.selectIdByPhone(phone);
        feedback.setUserId(id);
        if(feedback.getBeenUserId() == null){
            feedback.setType("1");
        }else{
            feedback.setType("0");
        }
        int i = feedbackMapper.insertSelective(feedback);
        log.info("返回的feedback:{}",feedback);



        if (i != 1){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }












    /****************        删       ******************************************************************/
    public void deleteByKey(Long id){
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = feedbackMapper.deleteByExample(feedbackExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }

    public void deleteByKeys(List<Long> id){
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();


        criteria.andIdIn(id);
        int i = feedbackMapper.deleteByExample(feedbackExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }







    /****************        改       ******************************************************************/
    public void updateByKey(Feedback feedback){
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        criteria.andIdEqualTo(feedback.getId());
        int i = feedbackMapper.updateByExampleSelective(feedback,feedbackExample);
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    /****************        查       ******************************************************************/
    public List<Feedback> getAllList(){
        FeedbackExample feedbackExample = new FeedbackExample();
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        return feedbacks;
    }


    public JsonPage<FeedbackVO> getListByExample(FeedbackSimpleDTO feedbackSimpleDTO){

        PageHelper.startPage(feedbackSimpleDTO.getPageNum(), feedbackSimpleDTO.getPageSize());

        FeedbackExample feedbackExample = new FeedbackExample();

        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);

        List<FeedbackVO> feedbackVOS = CopyUtil.copyList(feedbacks, FeedbackVO.class);
        for (Feedback feedback : feedbacks) {
            User user = userMapper.selectNameById(feedback.getUserId());
            User user1 = userMapper.selectNameById(feedback.getBeenUserId());
            for (FeedbackVO feedbackVO : feedbackVOS) {
                if(feedback.getId() == feedbackVO.getId()){
                    feedbackVO.setUserName(user.getNickname());
                    feedbackVO.setBeenUserName(user1.getNickname());
                }
            }
        }

        return JsonPage.restPage(new PageInfo<>(feedbackVOS));
    }



}